﻿// Groupon.NET C# Library
// Copyright (c) 2011 James Paul Duncan (JPaulDuncan)
// The MIT License (MIT)
// Permission is hereby granted, free of charge, to any person obtaining a 
// copy of this software and associated documentation files (the "Software"), 
// to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, 
// and/or sell copies of the Software, and to permit persons to whom the 
// Software is furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included 
// in all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE. 
// http://www.jpaulduncan.com

namespace Groupon.Net
{
    internal sealed class DealRequest : RequestBase
    {
        public DealRequest() : this("") { }

        public DealRequest(string dealId)
            : base()
        {
            this.GoogleAnalytics = new GoogleAnalytics();
            this.DealId = dealId;
        }

        public string DealId { get; set; }

        /// <summary>
        /// When not logged in, this can be used to load targeted deal content for a user.
        /// </summary>
        public string EmailAddress { get; set; }

        /// <summary>
        /// For divisions that have sub-areas, specify an area permalink to retrieve those deals.
        /// </summary>
        public string Area { get; set; }

        /// <summary>
        /// Determines which values should be returned
        /// </summary>
        public ShowTypes Show { get; set; }

        /// <summary>
        /// The uuXXXXX referral id for a Groupon user account. If provided, deal links returned by the API will include the proper UTM parameters to ensure that any purchases from that URL will be credited to that user's account
        /// </summary>
        public string ReferralId { get; set; }

        /// <summary>
        /// Optional parameters for google utm tracking
        /// </summary>
        public GoogleAnalytics GoogleAnalytics { get; set; }
        
        public override string RESTCall
        {
            get {

                base.ResetParameters();
                base.AddParameter("email_address", this.EmailAddress);
                base.AddParameter("area", this.Area);
                base.AddParameter("show", this.Show == ShowTypes.Default ? "default" : "all");
                base.AddParameter("referral_id", this.ReferralId);

                string parms = base.ParamsToQueryString();

                if (this.GoogleAnalytics != null)
                {
                    parms += "&" + this.GoogleAnalytics.ToURLParams();
                }

                string result = string.Empty;

                if (this.DealId.HasValue())
                {
                    result = string.Format(Client.APIUri.ToString() + @"deals.xml?{0}", parms);
                }
                else
                {
                    result = string.Format(Client.APIUri.ToString() + @"deals/{0}.xml?{1}", this.DealId, parms);
                }

                return result;
            }
        }

    }
}